package com.example.review;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: CustomProducerXync
 * Package: com.example.review
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-05
 * Time: 10:54
 */


//同步发送数据
public class CustomProducerSync {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //0.创建配置信息
        Properties properties = new Properties();

        //配置必要参数 连接信息
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"hadoop102:9092");

        //配置序列化器
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        //1.创建生产者
        KafkaProducer<String, String> kafkaProducer =
                new KafkaProducer<String, String>(properties);

        //发送消息 在发送数据的方法后面 在添加一个get方法就是 同步发送数据
        kafkaProducer.send(new ProducerRecord<>("first","Kafka")).get();

        //关闭资源
        kafkaProducer.close();
    }
}
